package cn.yunlang.zhangZhongFei.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.yunlang.zhangZhongFei.entity.Comment;
import cn.yunlang.zhangZhongFei.service.CommentService;
import cn.yunlang.zhangZhongFei.util.JwtUtil;
import cn.yunlang.zhangZhongFei.vo.request.CommentAdd;
import cn.yunlang.zhangZhongFei.vo.request.TokenRequest;
import cn.yunlang.zhangZhongFei.vo.response.R;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Comment")
@Api(tags = "公司评论表")
public class CommentController {

  @Resource private CommentService commentService;

  @ApiOperation("分页查评论")
  @GetMapping("/page")
  public R<IPage<Comment>> page(
      @ApiParam(value = "页码", required = true) @RequestParam Integer pageNo,
      @ApiParam(value = "每页大小", required = true) @RequestParam Integer pageSize,
      @ApiParam(value = "上级id，用于级联查询评论。默认为0(最顶级评论).最顶级评论会顺带查出5条子评论。")
          @RequestParam(required = false, defaultValue = "0")
          Long pId,
      @ApiParam(value = "评论内容", required = false) @RequestParam(required = false) String content) {
    return commentService.pageComment(pageNo, pageSize, pId, content);
  }

  @GetMapping("/one")
  @ApiOperation("根据Id查一条.最顶级评论会顺带查出5条子评论。")
  public R<Comment> one(@RequestParam Long commentId) {
    return commentService.getOne(commentId);
  }

  @PostMapping("/add")
  @ApiOperation("新增评论")
  public R<String> addTop(@RequestBody CommentAdd commentAdd) {
    commentAdd.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    return commentService.addTop(commentAdd);
  }

  @GetMapping("/delete")
  @ApiOperation("删除")
  public R<String> delete(@RequestParam Long commentId) {
    return commentService.deleteComment(commentId);
  }
}
